<?php

class Admin_Controllers_User extends Libs_Controller {

    function __construct() {
        parent::__construct();
    }

    public function index() {
        $this->view->render('user/index');
    }

    public function changePass() {
        @session_start();
        $email = $_SESSION['user_email'];
        $op = Hash::create($_POST['passOP']);
        $np = $_POST['passNP'];
        $np1 = $_POST['passNP1'];
        $user = new Admin_Models_User();
        $url = URL_BASE . "/admin/user";

        if ($user->checkPass($email, $op)) {
            if ($np == $np1) {
                $user->changePass($email, Hash::create($np));
                $out = "<script>";
                $out .= "alert('Đổi mật khẩu thành công')";
                $out .= "</script>";
                echo $out;
                $this->redirect($url);
            } else {
                $out = "<script>";
                $out .= "alert('Mật khẩu mới không khớp')";
                $out .= "</script>";
                echo $out;
                $this->redirect($url);
            }
        } else {
            $out = "<script>";
            $out .= "alert('Mật khẩu cũ không đúng')";
            $out .= "</script>";
            echo $out;
            $this->redirect($url);
        }
    }

    public function logout() {
        if (isset($_SESSION['user_email'])) {
            unset($_SESSION['user_email']);
            $url = URL_BASE;
            if (!empty($_SERVER['HTTP_REFERER'])) {
//                header("Location: " . $_SERVER['HTTP_REFERER']);
                echo "<script type='text/javascript'>window.history.go(-1)</script>";
            } else {
                header("Location: $url");
            }
            exit;
        } else {
            $url = URL_BASE;
            if (!empty($_SERVER['HTTP_REFERER'])) {
                header("Location: " . $_SERVER['HTTP_REFERER']);
            } else {
                header("Location: $url");
            }
            exit;
        }
    }

}
